#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "syntaxTree.h"

struct TreeNode *
tree_node_new(const char* name, int tipo)
{
    int i;
    struct TreeNode *node;

    node = (struct TreeNode *) malloc (sizeof(struct TreeNode));

    if (name != NULL){
        node->name = strdup(name);
    } else
        node->name = NULL;

    node->tipo = tipo;
    node->child_counter = 0;   
    node->parent = NULL;
    node->children = NULL;
    node->sibling = NULL;

    return node;
}

void
tree_node_add_child(struct TreeNode *self, struct TreeNode *child)
{
    struct TreeNode *temp;

    if (child == NULL)
        return;

    if (self->children == NULL) {
        child->parent = self;
        self->children = child;
    } else {
        tree_node_add_sibling(self->children, child);
    }
    for (temp = child; temp != NULL; temp = temp->sibling)
        temp->parent = self;
}

void
tree_node_add_sibling(struct TreeNode *self, struct TreeNode *sibling)
{
    struct TreeNode *temp;

    if (sibling == NULL)
        return;

    if (self->sibling == NULL) {
        self->sibling = sibling;
    } else {
        for (temp = self->sibling; temp->sibling != NULL; temp = temp->sibling)
            ;
        temp->sibling = sibling;
    }
}